package projorientado;

import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import projorientado.ocr.vfeature.impl.ImageLayerFeatures;
import projorientado.ocr.vfeature.SequentialFeatures;
import projorientado.ocr.vfeature.VisitorFeaturesComparator;

public class DebugVisitorComparator implements VisitorFeaturesComparator {

    private String outputFolder;

    public DebugVisitorComparator() {
    }

    public DebugVisitorComparator(String outputFolder) {
        this.outputFolder = outputFolder;
    }

    public String getOutputFolder() {
        return outputFolder;
    }

    public void setOutputFolder(String outputFolder) {
        this.outputFolder = outputFolder;
    }

    public void visit(int index, SequentialFeatures s1, SequentialFeatures s2, float value) {
        if(s1 instanceof ImageLayerFeatures && s2 instanceof ImageLayerFeatures) {
            File dirOut = new File(outputFolder + index);
            dirOut.mkdir();
            
            saveImage(outputFolder+index,"s1=" + value,s1);
            saveImage(outputFolder+index,"s2=" + value,s2);
        }
    }
    
    private static void saveImage(String path, String name, SequentialFeatures s1) {
        try {
            File s1o = new File(path + File.separatorChar + name + ".jpg");
            ImageIO.write(((ImageLayerFeatures) s1).toImageLayerData().toBWImage(), "JPG", s1o);
        } catch (IOException ex) {
            
        }
    }

    public void visitInfo(String info) {
        System.out.println(info);
    }
    
}
